/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.control;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.frm.BaseController;
import com.rf.sys.model.Dictionary;
import com.rf.sys.service.DictionaryService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;

@Controller
@RequestMapping("/sys/dictionary")
public class DictionaryController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(DictionaryController.class);
	private static final long serialVersionUID = -120160623160131906L;
	
	@Resource
	private DictionaryService dictionaryService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/sys/dictionary/dictionary_list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, Dictionary dictionary, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	Map searchMap = dictionaryService.getDictionaryPage(dictionary, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化信息
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		Dictionary dictionary = new Dictionary();
		if(id != null && !id.equals("")){
			dictionary = dictionaryService.getDictionaryByID(id);	
		}else{
			dictionary.setState(ConfigUtils.ENABLESTATE);
		}
		modelAndView.addObject("dictionary", dictionary);
		modelAndView.setViewName("/sys/dictionary/dictionary_edit");
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param Dictionary,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public void save (Dictionary dictionary, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String ret = "success";
		if(dictionary !=null && dictionary.getId()!=null && !dictionary.getId().equals("")){
			//修改
			dictionaryService.updateDictionary(dictionary);
		}else{
			//新增
			dictionaryService.addDictionary(dictionary);
		}
		super.resPrint(request, response, ret);//成功 
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("ids");
    	dictionaryService.deleteDictionary(ids);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param dictionary,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    public void updateState (Dictionary dictionary, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	dictionaryService.updateDictionary(dictionary);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 根据数据字典获取下拉
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception 
     */
	@RequestMapping(value = "dicbykey")
	@ResponseBody
	public void queryDicbykey(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String key=request.getParameter("key");
		String type=request.getParameter("type");
		Dictionary dictionary=new Dictionary();
		dictionary.setClassCode(key);
		List<Dictionary> list = dictionaryService.getDictionary(dictionary);
		
		List<Dictionary> rlist=new ArrayList<Dictionary>();
		if(!"1".equals(type)){
			Dictionary sysdic1=new Dictionary();
			sysdic1.setDataCode("");
			sysdic1.setDataName("");
			rlist.add(sysdic1);
			Dictionary sysdic=new Dictionary();
			sysdic.setDataCode("");
			sysdic.setDataName("请选择...");
			rlist.add(sysdic);
		}
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				rlist.add(list.get(i));
			}
		}
		
		
		JSONArray jsonarray = JSONArray.fromObject(rlist);  
		responsePrint(request,response, jsonarray.toString());
	}
	
	
	 /**
     * 根据数据字典获取下拉
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception 
     */
	@RequestMapping(value = "dicNamebykey", method = RequestMethod.GET)
	@ResponseBody
	public void queryValuebykey(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject returnObj = new JSONObject();
		String key=request.getParameter("key");
		String value=request.getParameter("value");
		Dictionary dictionary=new Dictionary();
		dictionary.setClassCode(key);
		dictionary.setDataCode(value);
		List<Dictionary> list = dictionaryService.getDictionary(dictionary);
		returnObj.element("httpCode", "OK");//成功
		if(list!=null&&list.size()>0){
			returnObj.element("keyName", list.get(0).getDataName());//成功
		}else{
			returnObj.element("keyName", "");//成功
		}
		
		super.responsePrint(request, response, returnObj.toString());
	}
}


